package com.zygh.lz.task;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zygh.lz.dao.PatrolrecordMapper;
import com.zygh.lz.dao.SignMapper;
import com.zygh.lz.entity.Patrolrecord;
import com.zygh.lz.entity.Sign;
import com.zygh.lz.service.impl.EquipageServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Component
public class SignOutTask {

    @Resource
    private SignMapper signMapper;
    @Resource
    private PatrolrecordMapper patrolrecordMapper;

    @Resource
    private EquipageServiceImpl equipageService;


    /**
     * 判断签到岗位的班次
     * 高速的是全天班，高架只上夜班
     * 上午班：7：30-13：00
     * 下午班：13：00-20：00
     * 全天班 高速
     * 夜班：20：00-次日7:30 高架
     * 1 代表上午班          13:30执行签退
     * 2 代表下午班          20：30执行签退
     * 3 代表夜班            次日08：00执行签退
     * 4 代表全天班和特勤    超过12个小时自动签退
     * <p>
     * 签到时间必须超过一个小时的，到时间未签退的才自动签退
     */


    //夜巡签退
//    @Async
//    @Scheduled(cron = "00 30 08 * * ? ")
    public void task1() {
        log.info("夜巡自动签退执行");
        Sign sign = new Sign();
        List<Map> mapList = signMapper.OnlineAll();
        for (Map map : mapList) {
            String shiftop = map.get("shiftop").toString();
            String comment = map.get("comment").toString();
            if ("3".equals(shiftop) && "1".equals(comment)) {
                sign.setId(Integer.valueOf(map.get("id").toString()))
                        .setSignouttime(DateUtil.now())
                        .setState(2);
                signMapper.updateByPrimaryKeySelective(sign);
                updatePatrole(map.get("signnum").toString());
            }

        }
    }


    //上午班签退
//    @Async
//    @Scheduled(cron = "00 00 14 * * ? ")
    public void task2() {
        log.info("上午班自动签退执行");
        Sign sign = new Sign();
        List<Map> mapList = signMapper.OnlineAll();
        for (Map map : mapList) {
            if (Objects.isNull(map.get("modeCode"))) {
                continue;
            }
            String shiftop = map.get("shiftop").toString();
            String comment = map.get("comment").toString();
            if ("1".equals(shiftop) && "1".equals(comment)) {
                sign.setId(Integer.valueOf(map.get("id").toString()))
                        .setSignouttime(DateUtil.now())
                        .setState(2);
                signMapper.updateByPrimaryKeySelective(sign);
                updatePatrole(map.get("signnum").toString());

            }

        }
    }


    //下午班签退
//    @Async
//    @Scheduled(cron = "00 00 21 * * ? ")
    public void task3() {
        log.info("下午班自动签退执行");
        Sign sign = new Sign();
        List<Map> mapList = signMapper.OnlineAll();
        for (Map map : mapList) {
            String shiftop = map.get("shiftop").toString();
            String comment = map.get("comment").toString();
            if ("2".equals(shiftop) && "1".equals(comment)) {
                sign.setId(Integer.valueOf(map.get("id").toString()))
                        .setSignouttime(DateUtil.now())
                        .setState(2);
                signMapper.updateByPrimaryKeySelective(sign);
                updatePatrole(map.get("signnum").toString());
            }

        }
    }


    ////特殊12小时自动签退
    @Async
    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void task4() {
        log.info("特殊勤务自动签退执行");
        Sign sign = new Sign();
        List<Map> mapList = signMapper.OnlineAll();
        for (Map map : mapList) {
            String comment = map.get("comment").toString();
            String signintime = map.get("signintime").toString();
            long hour = DateUtil.between(DateUtil.parseDateTime(signintime), new Date(), DateUnit.HOUR);
            if (hour >= 10 && "2".equals(comment)) {
                sign.setId(Integer.valueOf(map.get("id").toString()))
                        .setSignouttime(DateUtil.now())
                        .setState(2);
                signMapper.updateByPrimaryKeySelective(sign);
                updatePatrole(map.get("signnum").toString());
            }

        }
    }


    ////全天班24小时自动签退
//    @Async
//    @Scheduled(cron = "00 29 08 * * ? ")
    public void task5() {
        log.info("全天班自动签退执行");
        Sign sign = new Sign();
        List<Map> mapList = signMapper.OnlineAll();
        for (Map map : mapList) {
            String shiftop = map.get("shiftop").toString();
            String signintime = map.get("signintime").toString();
            long hour = DateUtil.between(DateUtil.parseDateTime(signintime), new Date(), DateUnit.HOUR);
            if (hour >= 5 && "4".equals(shiftop)) {
                sign.setId(Integer.valueOf(map.get("id").toString()))
                        .setSignouttime(DateUtil.now())
                        .setState(2);
                signMapper.updateByPrimaryKeySelective(sign);
                updatePatrole(map.get("signnum").toString());

            }

        }
    }


    private void updatePatrole(String signnum) {
        Patrolrecord patrolRecordOne = patrolrecordMapper.findPatrolRecordOne(signnum);
        if (patrolRecordOne != null) {
            patrolRecordOne.setPatrolRecordEndtime(new Date());
            patrolrecordMapper.updateByPrimaryKeySelective(patrolRecordOne);
        }

    }

    /**
     * 每天早上九点把昨天所有签到未签退的自动签退
     */
    @Async
    @Scheduled(cron = "0 0 9 * * ? ")
    public void task6() {
        log.info("每天早上九点自动签退所有昨天签到未签退执行");
        //获取昨天日期
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        //查询昨天所有签到未签退的
        QueryWrapper<Sign> signQueryWrapper = new QueryWrapper<>();
        signQueryWrapper.gt("signintime", yesterday + " 00:00:00");
        signQueryWrapper.lt("signintime", yesterday + " 23:59:59");
        signQueryWrapper.eq("state", 1);
        List<Sign> signList = signMapper.selectList(signQueryWrapper);
        log.info("昨天未签退：" + signList);
        for (Sign sign : signList) {
            //签退
            sign.setState(2).setSignouttime(DateUtil.now());
            signMapper.updateById(sign);
        }
    }

    @Async
    @Scheduled(cron = "0 0 5 * * ? ")
    public void autoUnbindingEquipment() {
        log.info("每天早上五点扫描绑定过期的装备，进行解绑");
        equipageService.autoUnbindingEquipment();
    }


    //签退
    /*private void signOut(Sign sign) {
        if (StringUtils.isNotEmpty(sign.getQdid())) {
            Location locationEnd = locationMapper.findLocationEnd(sign.getStaffid());
            String rwid;
            if ("2".equals(sign.getComment())) {
                Xlevelservice xleveByid = xlevelserviceMapper.selectByPrimaryKey(sign.getAssignmentid());
                rwid = xleveByid.getRwid();
            } else {
                Xarea xarea = xareaMapper.selectByPrimaryKey(sign.getAssignmentid());
                rwid = xarea.getRwid();
            }
            SignOut signOut = new SignOut();
            signOut.setQdid(sign.getQdid())
                    .setRwid(rwid)
                    .setUserid(sign.getWarningsignal());
            if (locationEnd != null) {
                signOut.setXxlat(locationEnd.getLongitude())
                        .setXxlng(locationEnd.getLatitude());
            }

            longFengService.sendSignOut(signOut);
        }
    }*/


}